//var SSO_COOKIE_URL = URL_UMS + "token.action";
//window.document.domain = "young.com";//设置域,与单点无关
/*jo.postJSONP(SSO_COOKIE_URL, function(json){
    var token = json.token;
    console.info("token = " + token);
});*/
/**
 * 登录
 */
function loginSubmit(){
    var account = $("#account").val();
    var password = $("#password").val();
    var securityCode = $("#securityCode").val();
    if(!jo.isValid(account) || !jo.isValid(password)){
        jo.alert("请填写用户名和密码!");
        return;
    }
    if(!jo.isValid(securityCode)){
        jo.alert("请填写验证码!");
        return;
    }
    if(!secured_session_id){
        jo.alert("安全认证无效,请刷新重试!");
        return;
    }

    jo.showLoading();
    var redirectTo = decodeURIComponent(jo.getUrlParam("redirectTo"));//登录成功后重定向的地址
    var fromUrl = decodeURIComponent(jo.getUrlParam("fromUrl"));//重定向到登陆页前用户的真正请求地址
    var base64 = new Base64();
    var encrypt = new JSEncrypt();
    encrypt.setPublicKey(SSO_RSA_PUBLIC_KEY);//公钥
    jo.postAjax(URL_LOGIN, {"account":encrypt.encrypt(base64.encode(account)),"password":encrypt.encrypt(base64.encode(password)),"securityCode":securityCode,"redirectTo":redirectTo, "sessionId":window.secured_session_id, fromUrl: fromUrl}, function(json){
        if(json.code == 0){
            //登录成功后缓存cookie(记住我)
            if(document.getElementById("rememberMe").checked){
                $.cookie(COOKIE_ACCOUNT, account, { expires: 7 });//有效期7天
            }else{//当没有勾选记住时,删除cookie
                $.removeCookie(COOKIE_ACCOUNT);
            }

            if(window.sessionStorage && json.token){//token有效时记录到本地存储
                sessionStorage.setItem(LOCAL_STORAGE_KEY_TOKEN, json.token);//把令牌存到本地存储
            }
            if(json.redirectTo){//重定向地址有效,则跳转
                window.location.href = json.redirectTo;
            }else{
                window.location.href = "/page/index.html";//默认跳转首页
            }

        }else{
            jo.alert(json.info);
            refreshSecurityCode();//刷新验证码
        }

    });
    jo.showLoading(false);
}
//安全认证下发的认证id,登录时需携带
window.secured_session_id = '';
/**
 * 安全认证
 */
function ssoSecured(){
    jo.postAjax(URL_SECURED, {}, function(json){
        if(json && json.code == 0 && json.data && json.data[0]) {
            var secured = json.data[0];
            window.secured_session_id = secured.sessionId;//保存认证id
            refreshSecurityCode();//刷新验证码
        }else{
            jo.showErrorMsg('安全认证失败');
        }
    }, true);
}
/**
 * 刷新验证码
 */
function refreshSecurityCode(){
    var sUrl = jo.parseUrl(URL_SECURITY_CODE + "/" + window.secured_session_id);//解析url
    $("#img_security_code").attr("src", sUrl);
    $('#img_security_code2').attr('src', sUrl);
}
/**
 * Base64 encode / decode
 * @constructor
 */
function Base64() {

    // private property
    _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    // public method for encoding
    this.encode = function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;
        input = _utf8_encode(input);
        while (i < input.length) {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
            output = output +
                _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
                _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
        }
        return output;
    }

    // public method for decoding
    this.decode = function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < input.length) {
            enc1 = _keyStr.indexOf(input.charAt(i++));
            enc2 = _keyStr.indexOf(input.charAt(i++));
            enc3 = _keyStr.indexOf(input.charAt(i++));
            enc4 = _keyStr.indexOf(input.charAt(i++));
            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output = output + String.fromCharCode(chr1);
            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }
        output = _utf8_decode(output);
        return output;
    }

    // private method for UTF-8 encoding
    _utf8_encode = function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            } else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            } else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }
        return utftext;
    }

    // private method for UTF-8 decoding
    _utf8_decode = function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}

//状态
var state = 1;//1登陆2注册

//打开注册
function openRegister(){
    //jo.showDialog($("#rg"), 'rg', '', {area:['400px','300px'],title:false,maxmin:false});
    $("#lg").hide();
    $("#rg").show();
    state = 2;
}
//打开登陆
function openLogin(){
    $("#rg").hide();
    $("#lg").show();
    state = 1;
}

//注册
function registerUser(){
    if(jo.checkForm($("#registerForm"))){
        var data = jo.form2Json("registerForm");
        if(data.password != data.password2){
            jo.showTipsMsg("两次密码输入不匹配!");
            return;
        }
        data.sessionId = window.secured_session_id;//安全会话id
        jo.postAjax("{URL_UMS}sso/register", data, function(json){
            if(json && json.code == 0){
                //注册成功,执行登录
                jo.showSuccessMsg('注册成功, 请登录!');
                refreshSecurityCode();//刷新验证码
                openLogin();
                $('#account').val(data.account);
                $('#password').val(data.password);
            }else if(json && json.code == -1){
                jo.showTipsMsg(json.info);
                refreshSecurityCode();//刷新验证码
            }else{
                jo.showErrorMsg('注册失败');
                refreshSecurityCode();//刷新验证码
            }
        });
    }
}